package ConcurrentProgramming;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 线程的优先级
 * 在操作系统中，可以为线程划分优先级，Cpu优先执行优先级高的线程
 * 在java中线程的优先级分为1-10，大于10或者小于1都会抛出illegalArgumentException()
 *
 *
 *
 */
class ThreadA extends Thread{
    @Override
    public void run() {
        System.out.println(currentThread().getName()+"---A");
    }
}

class ThreadB extends Thread{
    @Override
    public void run() {
        Thread.currentThread().setPriority(3);
        System.out.println(currentThread().getName()+"---B");
    }
}

class ThreadC extends Thread{
    @Override
    public void run() {
        Thread.currentThread().setPriority(6);
        for (int i = 0; i < 10; i++) {
            System.out.println(currentThread().getName()+"--->:"+i);
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class ThreadD extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(currentThread().getName()+"--->:"+i);
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}




public class Thread_Priority {
    public static void main(String[] args) {
        SetSimplePrority();
    }

    /**
     * 通过setPriortiy() 设置线程的优先级，数字越大，优先级越高
     * 此时的输出结果未必是优先级高的先执行，没有规律可言，注意设置的优先级只是
     * 让CPU倾向于去调度那些优先级高的，但是不能保证是一定的
     * 在绝大多数时间内，线程按照默认的优先级进行执行，试图操纵线程优先级通常是一种错误
     */
    public static void SetSimplePrority() {
        ThreadA threadA = new ThreadA();
        // threadA.setPriority(3);
        ThreadB threadB = new ThreadB();
        // threadB.setPriority(6);
        threadA.start();
        threadB.start();
    }

    /**
     * 大于10或者小于1都会抛出异常：IllegalArgumentException
     */
    public static void SetErrorPrority() {
        SecurityManager securityManager = new SecurityManager();
        ThreadA threadA = new ThreadA();
        securityManager.checkAccess(threadA);
        threadA.setPriority(4);
    }

    /**
     * 设置的优先级高的一定会先于优先级低的执行吗?
     * 结果：
     * Thread-0--->:0
     * Thread-1--->:0
     * Thread-1--->:1
     * Thread-1--->:2
     * Thread-1--->:3
     * Thread-0--->:1
     * Thread-1--->:4
     * Thread-1--->:5
     * Thread-1--->:6
     * Thread-1--->:7
     * Thread-1--->:8
     * Thread-1--->:9
     * Thread-0--->:2
     * Thread-0--->:3
     * Thread-0--->:4
     * Thread-0--->:5
     * Thread-0--->:6
     * Thread-0--->:7
     * Thread-0--->:8
     * Thread-0--->:9
     * 从结果可知并不一定是优先级高的先执行，上述出现了执行交错的现象，但是总体而言是先于优先级低的执行完
     * 但是执行的过程中可能优先级低的会在优先级高的之前执行
     */
    public static void TestMustProitory() {
        ThreadC threadC = new ThreadC();
        ThreadD threadD = new ThreadD();
        threadC.setPriority(3);
        threadD.setPriority(6); // 优先级高
        threadC.start();
        threadD.start();
    }
}
